using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JoyfulHomeJourney.BackEnd.Infrastructure.Atrributes;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Requests.Objects;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Responses;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Responses.Objects;
using JoyfulHomeJourney.BackEnd.Infrastructure.EFCore;
using JoyfulHomeJourney.BackEnd.Infrastructure.Tools;
using JoyfulHomeJourney.BackEnd.Server.EFCore;
using JoyfulHomeJourney.BackEnd.Server.Instances.Extends.Requests;
using JoyfulHomeJourney.BackEnd.Server.Instances.Extends.Responses;
using JoyfulHomeJourney.BackEnd.Server.Interfaces;
using Microsoft.EntityFrameworkCore;
namespace JoyfulHomeJourney.BackEnd.Server.Instances
{
/// <summary>
/// 一个类包含了服务类所需的所有仓库表的实例，仅提供给该服务类使用，请将所有服务类所需的仓库表的实例都放在该类中。
/// 仓库表的实例指提供了操作数据库指定表的所有方法的对象
/// </summary>
	internal class ActivityParticipantsEF
	{
		public ActivityParticipantsEF(DbContext dbContext)      
		{
			activityParticipantsServerEF = new ActivityParticipantsServerEF(dbContext);
            dictionarysServerEF = new DictionarysServerEF(dbContext);
        }
		public ActivityParticipantsServerEF activityParticipantsServerEF { get; set; }
        public DictionarysServerEF dictionarysServerEF { get; set; }
    }
	[InjectClass]
	public class ActivityParticipantsServer : IActivityParticipantsServer
	{
		private readonly ActivityParticipantsEF _activityParticipantsEF;//包含所有所需仓库表实现的对象
		// 请不要修改该构造方法,也无需添加属性，而应该在上面的EF类中添加属性和构造函数赋值语句
		public ActivityParticipantsServer(DbContext dbContext)
		{
			_activityParticipantsEF = new ActivityParticipantsEF(dbContext);
		}
		public async Task<ResponseApiDto> AddAsync(RequestActivityParticipantsDto dto)
		{
			return ResponseApiDto.Create(await _activityParticipantsEF.activityParticipantsServerEF.AddAsync(dto.ToCreateEntity()));
		}
		public async Task<ResponseApiDto> DeleteAsync(RequestActivityParticipantsDto dto)
		{
			return ResponseApiDto.Create(await _activityParticipantsEF.activityParticipantsServerEF.DeleteAsync(dto.ToDeleteEntity()));
		}
		public async Task<ResponseApiDto> UpdateAsync(RequestActivityParticipantsDto dto)
		{
			return ResponseApiDto.Create(await _activityParticipantsEF.activityParticipantsServerEF.UpdateAllProperNotNullAsync(dto.ToUpdateEntity()));
		}
		public async Task<ResponseApiDto> PaginationAsync(int page, int limit,string key,string cid)
		{
			int total = 0;
			key = key ?? "";
			List<ResponseActivityParticipantsDto> data = new List<ResponseActivityParticipantsDto>();
			List<ActivityParticipants> activityParticipantss = await _activityParticipantsEF.activityParticipantsServerEF.Pagination1(d=>d.ApName.Contains(key)&&d.ApEventId==cid,false, d => d.ApCreateAt, out total, page, limit).ToListAsync();
			foreach (var entity in activityParticipantss)
			{
				data.Add(new ResponseActivityParticipantsDto
				{
                    id = entity.ApId,
                    name = entity.ApName,
                    gender = entity.ApGender,
                    age = entity.ApAge,
                    phone = entity.ApPhone,
                    email = entity.ApEmail,
					industryName = await _activityParticipantsEF.dictionarysServerEF.Query(d => d.DId == entity.ApIndustry).Select(d => d.DValue).SingleOrDefaultAsync(),
                    industry = entity.ApIndustry,
                    eventId = entity.ApEventId,
                    createAt = entity.ApCreateAt,
                    createAtStr = Tools.TimeToString(entity.ApCreateAt),
                });
			}
			return ResponseApiDto.Success("数据已获取", new ResponsePaginationDto { total=total,data=data});
		}
	}
}
